﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib
{
    public class ScriptOptionsLoader : Interfaces.IProgressEvent, Interfaces.ISourceDatabaseCommand
    {
        private DBSourceToolsLib.DataSets.ScriptOptionsDS m_TableScriptOptionsDS
            = new DBSourceToolsLib.DataSets.ScriptOptionsDS();
        private DBSourceToolsLib.DataSets.ScriptOptionsDS m_ProcScriptOptionsDS
            = new DBSourceToolsLib.DataSets.ScriptOptionsDS();

        public DBSourceToolsLib.DataSets.ScriptOptionsDS TableScriptOptionsDS
        {
            get { return m_TableScriptOptionsDS; }
            set { m_TableScriptOptionsDS = value; }
        }
        public DBSourceToolsLib.DataSets.ScriptOptionsDS ProcScriptOptionsDS
        {
            get { return m_ProcScriptOptionsDS; }
            set { m_ProcScriptOptionsDS = value; }
        }

        SourceDatabase m_SourceDatabase;
        Form m_ParentForm;
        Commands.DatabaseCommandTypes m_CommandType;

        private string m_ErrorMessage = null;
        private bool m_HasErrors = false;

        public ScriptOptionsLoader(Form oForm
            , Commands.DatabaseCommandTypes oCommandType)
        {
            m_ParentForm = oForm;
            m_CommandType = oCommandType;
        }

        #region IProgressEvent Members

        public event EventHandler<ProgressEventArgs> ProgressEventPublisher;

        public void Execute()
        {
            EventHandler<ProgressEventArgs> localHandler = ProgressEventPublisher;

            switch (m_CommandType)
            {
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadTableList:
                    this.m_TableScriptOptionsDS = m_SourceDatabase.ScriptOptions.LoadTablesFromDB(m_SourceDatabase, localHandler, false);
                    break;
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadStoredProcList:
                    this.m_ProcScriptOptionsDS = m_SourceDatabase.ScriptOptions.LoadStoredProcsFromDB(m_SourceDatabase, localHandler, false);
                    break;
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadViewList:
                    this.m_ProcScriptOptionsDS = m_SourceDatabase.ScriptOptions.LoadViewsFromDB(m_SourceDatabase, localHandler, false);
                    break;
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadUserList:
                    this.m_ProcScriptOptionsDS = m_SourceDatabase.ScriptOptions.LoadUsersFromDB(m_SourceDatabase, localHandler, false);
                    break;
            }

            if (localHandler != null)
                localHandler(this, new ProgressEventArgs(100, 100,
                    "Processing Complete"));

        }

        #endregion

        #region ISourceDatabaseCommand Members

        public void Execute(ref SourceDatabase oRefSourceDatabase, string strParams)
        {
            m_SourceDatabase = oRefSourceDatabase;
            UI.Forms.ProgressForm oProgressForm = new DBSourceToolsLib.UI.Forms.ProgressForm();
            oProgressForm.setEventSource(this);
            oProgressForm.ShowDialog(m_ParentForm);

        }

        #endregion

        #region IProgressEvent Members


        public bool HasErrors()
        {
            return m_HasErrors;
        }

        public string GetErrorMessage()
        {
            return m_ErrorMessage;
        }

        #endregion

        #region IProgressEvent Members


        public void RegisterError(string strErrorMessage)
        {
            this.m_ErrorMessage = strErrorMessage;
        }

        #endregion
    }
}
